package day14;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(9999);
        while (true) {
            Socket client = server.accept();//等待客户连接
            //通过线程 模拟每个客户端连接之后读写的逻辑
            new ServerThread(client).start();

        }
    }
}

//只负责 接收 每个客户端写入的数据
class ServerThread extends Thread {
    Socket client;

    public ServerThread(Socket client) {
        this.client = client;
    }

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(client.getInputStream()));
            while (true) {
                String msg = br.readLine();
                                    //getInetAddress    获取IP地址
                System.out.println(client.getInetAddress() + ":" + msg);
                //System.out.println(Thread.currentThread().getName());
            }
        } catch (Exception e) {}
    }
}